import {
	IAbility,
	IContext,
	IParam,
	verifyNodeLinkCond,
	deepCopy,
	typeOf,
	UILogicParamService,
	UILogicParamType,
} from '@/core';
/**
 * 打开人员管理
 * 基于template/src/ui-service/{{appEntities}}/{{uiLogics}}-ui-logic-base.ts.hbs生成
 * @export
 * @class ManageUILogicBase
 */
export class ManageUILogicBase {
	/**
	 * 返回结果
	 *
	 * @private
	 * @type {*}
	 * @memberof ManageUILogicBase
	 */
	private result: any;

	/**
	 * 行为附加参数
	 *
	 * @type {*}
	 * @memberof ManageUILogicBase
	 */
	public additionalParam: IParam = {};

	/**
	 * 界面逻辑上下文
	 *
	 * @private
	 * @type {IContext}
	 * @memberof ManageUILogicBase
	 */
	private appContext!: IContext;

	/**
	 * 界面逻辑视图参数
	 *
	 * @private
	 * @type {IParam}
	 * @memberof ManageUILogicBase
	 */
	private viewParam!: IParam;

	/**
	 * 默认逻辑处理参数名称
	 *
	 * @private
	 * @type {string}
	 * @memberof ManageUILogicBase
	 */
	private defaultParamName: string = '';

	/**
	 * 应用上下文参数名称
	 *
	 * @private
	 * @type {string}
	 * @memberof ManageUILogicBase
	 */
	private navContextParamName: string = '';

	/**
	 * 视图参数名称
	 *
	 * @private
	 * @type {string}
	 * @memberof ManageUILogicBase
	 */
	private navViewParamParamName: string = '';

	/**
	 * 当前容器参数名称
	 *
	 * @private
	 * @type {string}
	 * @memberof ManageUILogicBase
	 */
	private activeContainerParamName: string = '';

	/**
	 * 当前激活部件参数名称
	 *
	 * @private
	 * @type {string}
	 * @memberof ManageUILogicBase
	 */
	private activeCtrlParamName: string = '';

	/**
	 * 当前激活视图对象名称
	 *
	 * @private
	 * @type {string}
	 * @memberof ManageUILogicBase
	 */
	private activeViewParamName: string = '';

	/**
	 * 上一次调用返回参数名称
	 *
	 * @private
	 * @type {string}
	 * @memberof ManageUILogicBase
	 */
	private lastReturnParamName: string = '';

	/**
	 * 逻辑参数Map
	 *
	 * @private
	 * @type {Map<string, IParam>}
	 * @memberof ManageUILogicBase
	 */
	private logicParamMap: Map<string, IParam> = new Map();

	/**
	 * 绑定上一次调用返回参数
	 *
	 * @memberof ManageUILogicBase
	 */
	public bindLastReturnParam(value: any) {
		const lastReturnParam = this.logicParamMap.get(this.lastReturnParamName);
		if (lastReturnParam) {
			lastReturnParam.bind(value);
		}
	}

	/**
	 * 默认逻辑处理参数
	 *
	 * @memberof ManageUILogicBase
	 */
	get defaultParam(): any {
		return this.logicParamMap.get(this.defaultParamName);
	}

	/**
	 * 上下文数据（包括应用上下文和逻辑局部上下文参数）
	 *
	 * @readonly
	 * @memberof ManageUILogicBase
	 */
	get context() {
		const navContextParam = this.logicParamMap.get(this.navContextParamName);
		if (navContextParam) {
			return navContextParam.getReal();
		} else {
			return this.appContext;
		}
	}

	/**
	 * 视图参数数据（包括外部传入视图参数和逻辑局部视图参数）
	 *
	 * @readonly
	 * @memberof ManageUILogicBase
	 */
	get viewParams() {
		const navViewParamParam = this.logicParamMap.get(
			this.navViewParamParamName
		);
		if (navViewParamParam) {
			return navViewParamParam.getReal();
		} else {
			return this.viewParam;
		}
	}

	/**
	 * 获取逻辑处理参数
	 *
	 * @param {string} key 逻辑处理参数的codeName
	 * @memberof UIActionContext
	 */
	private getParam(key: string): any {
		return this.logicParamMap.get(key);
	}

	/**
	 * Creates an instance of ManageUILogicBase
	 *
	 * @param {IParam} [opts={}]
	 * @memberof ManageUILogicBase
	 */
	constructor(opts: IParam = {}) {}

	/**
	 * 逻辑参数模型
	 *
	 * @private
	 * @type {any[]}
	 * @memberof ManageUILogicBase
	 */
	private logicParamModels: any[] = [
		{
			name: 'openarg',
			codeName: 'openarg',
			entityParam: true,
		},
		{
			name: '传入变量',
			codeName: 'Default',
			default: true,
			entityParam: true,
		},
	];

	/**
	 * 初始化逻辑参数
	 * @param context 应用上下文
	 * @param viewParams 视图参数
	 * @param data 业务数据
	 * @param event 事件对象
	 * @param ability 当前环境能力
	 * @memberof ManageUILogicBase
	 */
	private initLogicParams<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam,
		$event: MouseEvent,
		ability: T
	) {
		this.appContext = deepCopy(context);
		this.viewParam = deepCopy(viewParams);
		this.additionalParam = {
			data,
			$event,
			ctrl: ability.viewCtx.ctrl,
			actionContext: ability,
		};
		this.logicParamModels.forEach((model: IParam) => {
			if (model.default) {
				this.defaultParamName = model.codeName;
			}
			if (model.navContextParam) {
				this.navContextParamName = model.codeName;
			}
			if (model.navViewParamParam) {
				this.navViewParamParamName = model.codeName;
			}
			if (model.activeContainerParam) {
				this.activeContainerParamName = model.codeName;
			}
			if (model.activeCtrlParam) {
				this.activeCtrlParamName = model.codeName;
			}
			if (model.activeViewParam) {
				this.activeViewParamName = model.codeName;
			}
			if (model.lastReturnParam) {
				this.lastReturnParamName = model.codeName;
			}
			const uiLogicParamInstance = UILogicParamService.getLogicParamInstance(
				this,
				model,
				{
					data,
					context,
					viewParams,
					$event,
					ctrl: ability.viewCtx.ctrl,
					actionContext: ability,
				}
			);
			this.logicParamMap.set(model.codeName, uiLogicParamInstance);
		});
	}

	/**
	 * 执行界面逻辑之前
	 * @param context 应用上下文
	 * @param viewParams 视图参数
	 * @param data 业务数据
	 * @param event 事件对象
	 * @param ability 当前环境能力
	 * @memberof ManageUILogicBase
	 */
	private beforeExecute<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam,
		event: MouseEvent,
		ability: T
	) {
		this.initLogicParams(context, viewParams, data, event, ability);
	}

	/**
	 * 执行界面逻辑
	 * @param context 应用上下文
	 * @param viewParams 视图参数
	 * @param data 业务数据
	 * @param event 事件对象
	 * @param ability 当前环境能力
	 * @memberof ManageUILogicBase
	 */
	public async execute<T extends IAbility>(
		context: IContext,
		viewParams: IParam,
		data: IParam,
		event: MouseEvent,
		ability: T
	) {
		try {
			this.beforeExecute(context, viewParams, data, event, ability);
			await this.execute_Begin_node();
			return this.result;
		} catch (error: any) {
			throw new Error(
				`${error && error.message ? error.message : '发生未知错误！'}`
			);
		}
	}

	/**
	 * 执行 开始 节点
	 *
	 * @protected
	 * @memberof ManageUILogicBase
	 */
	protected async execute_Begin_node() {
		this.result = this.defaultParam.getReal();
		console.log(`已完成执行 开始节点，操作参数数据如下:`);
		if (this.logicParamMap && this.logicParamMap.size > 0) {
			for (let [key, value] of this.logicParamMap) {
				console.log(key, deepCopy(value.getReal()));
			}
		}
		console.log(`即将执行准备参数节点`);
		await this.execute_PREPAREJSPARAM1_node();
	}

	/**
	 * 执行 准备参数 节点
	 *
	 * @protected
	 * @memberof ManageUILogicBase
	 */
	protected async execute_PREPAREJSPARAM1_node() {
		try {
			//  目标数据
			const dstParam_0: any = this.getParam('openarg');
			let result_0: any = null;
			const srcParam_0 = this.getParam('Default');
			result_0 = srcParam_0.get('id') || null;
			dstParam_0.set('orgid', result_0);
			console.log(`已完成执行 准备参数节点，操作参数数据如下:`);
			if (this.logicParamMap && this.logicParamMap.size > 0) {
				for (let [key, value] of this.logicParamMap) {
					console.log(key, deepCopy(value.getReal()));
				}
			}
			console.log(`即将执行界面行为节点`);
			await this.execute_DEUIACTION1_node();
		} catch (error: any) {
			throw new Error(
				`执行 准备参数 节点 ${
					error && error.message ? error.message : '发生未知错误!'
				}`
			);
		}
	}

	/**
	 * 执行 界面行为 节点
	 *
	 * @protected
	 * @memberof ManageUILogicBase
	 */
	protected async execute_DEUIACTION1_node() {
		try {
			const data = this.getParam('openarg').getReal();
			const { $event, actionContext } = this.additionalParam;
			const uiService = await App.getUIService(
				'Sys_organization',
				this.context
			);
			if (
				uiService &&
				uiService.openOrgPerson &&
				uiService.openOrgPerson instanceof Function
			) {
				const result = await uiService.openOrgPerson(
					this.context,
					this.viewParams,
					Array.isArray(data) ? data : [data],
					$event,
					actionContext
				);
				if (result.ok) {
					this.bindLastReturnParam(result.data);
				}
			}
			console.log(`已完成执行 界面行为节点，操作参数数据如下:`);
			if (this.logicParamMap && this.logicParamMap.size > 0) {
				for (let [key, value] of this.logicParamMap) {
					console.log(key, deepCopy(value.getReal()));
				}
			}
			console.log(`即将执行结束节点`);
			await this.execute_END1_node();
		} catch (error: any) {
			throw new Error(
				`执行 界面行为 节点 ${
					error && error.message ? error.message : '发生未知错误！'
				}`
			);
		}
	}

	/**
	 * 执行 结束 节点
	 *
	 * @protected
	 * @memberof ManageUILogicBase
	 */
	protected async execute_END1_node() {}
}
